#include <stdio.h>
#include "epoll.h"
#include <pthread.h>  

using namespace std;

socket_data *g_connect = NULL;
socket_data *g_connect1 = NULL;
cepoll_data *g_epoll = NULL;

void *readline_stdin(void *arg) 
{
	char tmp[1024];
	while (!feof(stdin)) {
		if (fgets(tmp,sizeof(tmp),stdin) == NULL) 
		{
			// read stdin failed
			LOG_DBG("stdin error");
			break;
		}
		int n  = strlen(tmp) -1;
		tmp[n] = 0;
		char *result = pack_data(tmp,n);
		send_data(g_epoll,g_connect,result,n+2);
	}
	return NULL;
}

//et epoll
int main(int argc,char *argv[])
{
	if(argc != 3)
	{
		LOG_DBG("param one ip,tow port");
		return 0;
	}
	const char *ip = argv[1]; 
	int port = atoi(argv[2]);
	g_epoll = create_epoll();
	g_connect = connect_sock(g_epoll,ip,port);
    g_connect1 = connect_sock(g_epoll,ip,port);
	epoll_event evs[EPOLL_EVENT_MAX];
	pthread_t pid;
	pthread_create(&pid,NULL,readline_stdin,NULL);
	while(true)
	{
		epoll_loop(g_epoll,evs,EPOLL_EVENT_MAX);
	}
	release_epoll(g_epoll);
	return 0;	
}
